<?php
/**
 * @version		$Id: helper.php 2011-07-05 vinaora $
 * @package		VINAORA SLICK SLIDESHOW
 * @subpackage	mod_vinaora_slickshow
 * @copyright	Copyright (C) 2010 - 2011 VINAORA. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 * @website		http://vinaora.com
 * @twitter		http://twitter.com/vinaora
 * @facebook	http://facebook.com/vinaora
 */

// no direct access
defined('_JEXEC') or die;

require_once dirname(__FILE__).DS."helper".DS."vinaora_item.php";
require_once dirname(__FILE__).DS."helper".DS."vinaora_param.php";

class modVinaoraSlickshowHelper
{
	/*
	 * Get content of the config file
	 */
	public static function getConfig($name){

		$name = JPath::clean($name);
		
		if ( !is_file(JPATH_BASE.DS.$name) ) return NULL;

		// Load from file if it is .xml
		if ( strtolower(substr($name, -4, 4)) == '.xml' ){
			$xml = simplexml_load_file( JPATH_BASE.DS.$name );
		}
		// Load from URL if it is .xml.php
		else{
			$xml = simplexml_load_file( JURI::base().JPath::clean($name, '/') );
		}
		
		//TODO File exits but not valid XML
		
		return $xml;

	}
	
	/*
	 * Create the config file
	 */
	public static function createConfig($name, $params){

		jimport('joomla.filesystem.file');
		$name = JPath::clean($name);
		$name = JPATH_BASE.DS.$name;

		if ( is_writeable(dirname($name)) ){
			if ( JFile::write($name, self::_createXML($params)) ) return true;
			else{
				// TODO: Write file error
			}
		}
		else{
			// TODO: Folder is not writeable
		}

		return false;

	}

	/*
	 * Create content of the config file
	 */
	private static function _createXML($params){
		$xml = '<?xml version="1.0" encoding="utf-8"?>';
		
		// Create Element - <banner>
		$node = new SimpleXMLElement($xml.'<banner />');
		
		// Create General Settings
		$node = &self::_createGeneral($node, $params);
		
		// Create Items
		$node = &self::_createItems($node, $params);
		
		$xml = $node->asXML();
		$xml = str_replace('slideshowtime>', 'slideShowTime>', $xml);

		return $xml;
	}
	
	
	/*
	 * Create General Settings
	 */
	private static function &_createGeneral(&$node, $params){
	
		$attbs = "width|height|backgroundColor|backgroundTransparency|startWith|barHeight|fadeTransition|verticalTransition|controllerTop|transitionSpeed|titleX|titleY";
		$attbs = explode("|", $attbs);
		foreach ($attbs as $attb){
			$node->addAttribute($attb, $params->get($attb));
		}

		return $node;
	}
	
	/*
	 * Create Items
	 */
	private static function &_createItems(&$node, $params){
		// Create Element - <banner>.<items>
		$nodeL1 = &$node->addChild('items');
		$items = explode("\n", $params->get('item_path'));
		foreach ($items as $key=>$value){
			$nodeL1 = &self::_createItem($nodeL1, $params, $key+1);
		}
		
		return $node;
	}
	
	/*
	 * Create a Item Element
	 */
	private static function &_createItem(&$node, $params, $position=1){
		// Create Element - <banner>.<items>.<item>
		$nodeL1 = &$node->addChild('item');
		
		// Create Element - <banner>.<items>.<item>.<title>
		$param	= $params->get('item_title');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		$param	= trim($param);
		$nodeL2	= &$nodeL1->addChild('title', $param);
		
		// Create Element - <banner>.<items>.<item>.<path>
		$param	= $params->get('item_path');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		$param	= trim($param);
		$nodeL2	= &$nodeL1->addChild('path', $param);

		// Create Element - <banner>.<items>.<item>.<url>
		$param	= $params->get('item_url');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		$param	= trim($param);
		$nodeL2	= &$nodeL1->addChild('url', $param);

		// Create Element - <banner>.<items>.<item>.<target>
		$param	= $params->get('item_target');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		$param	= trim($param);
		$param	= extVinaoraParam::validTarget($param);
		$nodeL2	= &$nodeL1->addChild('target', $param);
		
		// Create Element - <banner>.<items>.<item>.<bar_color>
		$param	= $params->get('item_bar_color');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		$param	= trim($param);
		if (empty($param)){
			$param	= $params->get("barColor");
		}
		else{
			$param	= extVinaoraParam::validColor($param, substr($params->get("barColor"), -6, 6), "0x");
		}
		
		$nodeL2	= &$nodeL1->addChild('bar_color', $param);
		
		// Create Element - <banner>.<items>.<item>.<bar_transparency>
		$param	= $params->get('item_bar_transparency');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		
		if (empty($param)){
			$param = $params->get("barTransparency");
		}
		else{
			$param	= extVinaoraParam::validTransparency($param);
		}
		
		$nodeL2	= &$nodeL1->addChild('bar_transparency', $param);

		// Create Element - <banner>.<items>.<item>.<slideShowTime>
		$param	= $params->get('item_slideShowTime');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		$param	= trim($param);
		if ($param == ""){
			$param = $params->get("slideShowTime");
		}
		else{
			$param	= abs(intval($param));
		}
		$nodeL2	= &$nodeL1->addChild('slideShowTime', $param);
		
		return $node;
	}
	
	/*
	 * Get List of Items from a Folder
	 */
	private static function _getItems($folder, $fullpath=false){

		$root		= JPATH_BASE.DS.'images';
		$default	= JPATH_BASE.DS.'media'.DS.'mod_vinaora_slickshow'.DS.'images'.DS.'default';
		
		$filter 	= '\.png$|\.gif$|\.jpg$|\.bmp$';
		$exclude	= array('index.html', '.htaccess');
		
		$items		= extVinaoraItem::getItems($folder, $root, $default, $filter, $exclude, $fullpath);
		
		return $items;

	}
	
	/*
	 * Get Item Path
	 */
	private static function _getItemPath($folder){
		
		$root		= 'images/';
		$default	= MEDIA_URL.'images/default';
		
		$path		= extVinaoraItem::getItemPath($folder, $root, $default);
		
		return $path;
		
	}
	
	/*
	 * Get List of Item URLs
	 */
	public static function getItemPaths($folder){
		$path 	= self::_getItemPath($folder);
		$items	= self::_getItems($folder);
		
		if (count($items)){
			foreach($items as $key=>$value){
				$items[$key] = $path.'/'.$value;
			}
		}

		return $items;
	}

}
